/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package moviedownloader5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.apache.commons.dbutils.DbUtils;

/**
 *
 * @author patrik
 */
    public class SqlStatement {
        
        private final DataSource ds;
        private Connection conn;

        public SqlStatement(DataSource ds) {
            Objects.requireNonNull(ds);
            this.ds = ds;
        }
        
        
        public ResultSet executeQuery(String string) {
            conn = null;
            PreparedStatement ps = null;
            try {
                conn = ds.getConnection();
                ps = conn.prepareStatement(string);
                return ps.executeQuery();
            } catch (SQLException ex) {
                Logger.getLogger(MovieToDatabase.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if(ps != null){
                    DbUtils.closeQuietly(ps);
                }
                if(conn != null){
                    DbUtils.closeQuietly(conn);
                }
            }
            return null;           
        }

        public Connection getConnection() {
            try {
                return ds.getConnection();
            } catch (SQLException ex) {
                Logger.getLogger(MovieToDatabase.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }
        
        
    }
